package com.bltec.emergency.controller.resController;

import com.alibaba.fastjson.JSON;
import com.bltec.emergency.common.Constant;
import com.bltec.emergency.common.ResponseJson;
import com.bltec.emergency.model.ResExternal;
import com.bltec.emergency.service.resService.ResExternalService;
import com.bltec.emergency.service.mgrService.UserService;
import com.bltec.emergency.utils.DateUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/resExternal")
@RequiresPermissions("project:res")
public class ResExternalController {
    @Resource
    private ResExternalService resExternalService;
    @Resource
    private UserService userService;
    /**
     * 获得所有
     */
    @RequestMapping("getAllResExternal")
    public ResponseJson getTrain() {
        try {
            List<ResExternal> resExternal = resExternalService.getAll();
            return new ResponseJson(Constant.OK, JSON.toJSON(resExternal));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseJson(Constant.FAIL, e.getMessage());
        }
    }

    /**
     * 添加
     * #{trainName},#{trainUnit},#{trainDepartment},#{trainTime},#{trainPosition},#{participantsNum},#{createBy},#{createTime},#{enable}
     */
    @RequiresPermissions("res:insert")
    @RequestMapping("addResExternal")
    public ResponseJson addTrain(String medical, String communication, String transport, String shelter) {
        try {
            ResExternal resExternal = new ResExternal();
            resExternal.setMedical(medical);
            resExternal.setCommunication(communication);
            resExternal.setTransport(transport);
            resExternal.setShelter(shelter);
            resExternal.setEnable(true);
            resExternal.setCreateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            resExternal.setCreateTime(DateUtil.dateNow());
            resExternalService.add(resExternal);
            return new ResponseJson(Constant.OK, "创建用户成功");
        } catch (Exception e) {
            return new ResponseJson(Constant.FAIL, "创建用户失败" + e.getMessage());
        }
    }

    /**
     * 修改
     */
    @RequiresPermissions("res:update")
    @RequestMapping("updateResExternal")
    public ResponseJson updateTrain(Long id, String medical, String communication, String transport, String shelter) {
        try {
            ResExternal resExternal = resExternalService.getResExternalById(id);
            resExternal.setMedical(medical);
            resExternal.setCommunication(communication);
            resExternal.setTransport(transport);
            resExternal.setShelter(shelter);
            resExternal.setUpdateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            resExternal.setUpdateTime(DateUtil.dateNow());
            resExternalService.updateResExternalById(resExternal);
            return new ResponseJson(Constant.OK, JSON.toJSON(resExternal));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseJson.UPDATE_ERROR;
        }
    }

    /**
     * 删除
     */
    @RequiresPermissions("res:delete")
    @RequestMapping("deleteResExternal")
    public ResponseJson setDisable(Long id) {
        if (null != resExternalService.getResExternalById(id)) {
            if (resExternalService.deleteResExternalById(id)) {
                return new ResponseJson(Constant.OK, "删除成功");
            } else {
                return new ResponseJson(Constant.FAIL, "删除失败");
            }
        } else {
            return new ResponseJson(Constant.OK, "删除成功");
        }
    }
}
